package com.itheima.xx.dao;

import com.itheima.xx.pojo.User;
import com.itheima.xx.util.SelectProviderUtil;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/10/26 15:33
 */
public interface UserMapper {
    /**
     * 查询id为1的用户及其订单信息
     * 用户id值是1，只有一个用户。一个用户可以有多个订单。所以这里是一对多关系
     */
    User queryOneToMany(@Param("id") Long id);

    @Insert("insert into tb_user values(null,#{userName},#{password},#{name},#{age},#{sex})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void addUser(User user);

    @Delete("delete from tb_user where id=#{id}")
    void deleteUserById(@Param("id") long id);

    @Update("update tb_user set user_name = #{userName},password=#{password} where id = #{id}")
    void updateUserById(User user);

    @Select("select * from tb_user where id = #{id}")
    /**
     *   @Results相当于映射xml文件的resultMap标签
     *      1.数据表字段名和实体类成员变量名不一致
     *      2.多表查询
     */
    @Results({@Result(id = true, column = "id", property = "id"),@Result(column = "user_name",property = "userName")})
    User queryUserById(@Param("id") long id);

    /**
     * select * from tb_user where sex = 1 and user_name like '%%'
     */
    @SelectProvider(type = SelectProviderUtil.class,method="queryUsersBySexOrUserName")
    List<User> queryUsersBySexOrUserName(@Param("userName") String userName);
}
